import QtQuick 2.0
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.0


Item {
    objectName: "TcpSwarm"
    width: 800
    height: 600

    property var comlist: []
    visible: true
    property bool uart_open: false
    property var showbuf: ""
    property var lua_script_text: ""

    ColorAnimation {
        from: "white"
        to: "black"
        duration: 200
    }
    Rectangle{
        color: "#aeaeae"
        anchors.rightMargin: 0
        anchors.bottomMargin: 0
        anchors.leftMargin: 0
        anchors.topMargin: 0
        anchors.fill: parent
        scale: 1
        transformOrigin: Item.Center

        GridLayout {
            width: parent.width
            height: parent.height
            x: 0
            y: 0
            rows: 3
            columns: 2

            Label {
                id: label
                text: qsTr("lua脚本:")
                Layout.columnSpan: 1
                font.pixelSize: 30
                font.bold: true
            }
            Label {
                id: label2
                text: qsTr("输出:")
                Layout.columnSpan: 1
                font.pixelSize: 30
                font.bold: true
            }
            Flickable {
                id: flick
                Layout.preferredHeight: parent.height - 100
                Layout.preferredWidth: parent.width/2 - 20
                Layout.columnSpan: 1

                contentWidth: lua_script.paintedWidth
                contentHeight: lua_script.paintedHeight
                clip: true
                Layout.leftMargin: 10

                ColorAnimation {
                    from: "white"
                    to: "white"
                    duration: 200
                }
                function ensureVisible(r)
                {
                    if (contentX >= r.x)
                        contentX = r.x;
                    else if (contentX+width <= r.x+r.width)
                        contentX = r.x+r.width-width;
                    if (contentY >= r.y)
                        contentY = r.y;
                    else if (contentY+height <= r.y+r.height)
                        contentY = r.y+r.height-height;
                }

                TextEdit {
                    id: lua_script
                    focus: true
                    text: lua_script_text
                    font.pixelSize: 20
                    color: "red"
                    selectByMouse : true
                    wrapMode: TextEdit.Wrap
                    onCursorRectangleChanged: flick.ensureVisible(cursorRectangle)
                }
            }

            TextArea {
                id: text_output
                Layout.preferredHeight: parent.height - 100
                text: qsTr("")
                font.pixelSize: 20
                Layout.columnSpan: 1
                Layout.preferredWidth: parent.width/2 - 20
                background: Rectangle {
                    border.color: text_output.enabled ? "#21be2b" : "transparent"
                }
                Layout.leftMargin: 10

            }
            RowLayout{
                visible: true
                Layout.columnSpan: 2
                Button {
                    id: button2
                    text: qsTr("保存lua脚本")
                    Layout.preferredHeight: 41
                    Layout.preferredWidth: 116
                    Layout.leftMargin: 15
                    Layout.bottomMargin: 10

                    onClicked: {
                        lua_script_text = lua_script.text
                        let ret = DataWrap.saveLuaScript(lua_script_text)
                        console.log(ret)
                        if(ret != 0){
                            tip.text = "保存lua脚本失败"
                            tip.visible = true
                            return
                        }
                        tip.text = "保存lua脚本成功"
                        tip.visible = true
                    }
                }
                Button {
                    id: button
                    text: qsTr("更新lua脚本")
                    Layout.preferredHeight: 41
                    Layout.preferredWidth: 116
                    Layout.bottomMargin: 10

                    onClicked: {
                        lua_script_text = lua_script.text
                        let ret = DataWrap.updateLuaScript(lua_script_text)
                        if(ret != 0){
                            tip.text = "更新lua脚本失败"
                            tip.visible = true
                            return
                        }
                        tip.text = "更新lua脚本成功"
                        tip.visible = true
                    }

                }

            }
        }
    }
    ToolTip {
        id: tip
        text: qsTr("")
        visible: false
        contentItem: Text {
            text: tip.text
            font: tip.font
            color: "#21be2b"
        }

        background: Rectangle {
            border.color: "#21be2b"
        }
        delay: 100
        timeout: 1000
    }
    function addString(str){
        showbuf += str;
        text_output.text  = showbuf;
    }
}
